from selenium import webdriver
from time import sleep
from selenium.webdriver import ChromeOptions, ActionChains


class Tb:

    def __init__(self):
        self.option = ChromeOptions()
        self.option.add_experimental_option('excludeSwitches', ['enable-automation'])
        self.option.add_argument("--disable-blink-features")
        self.option.add_argument("--disable-blink-features=AutomationControlled")
        self.option.add_argument("--window-size=1960,1080")
        self.driver = webdriver.Chrome("E:\chromedriver_win32\chromedriver.exe", options=self.option)

    # 四季星座网-商品发布
    def sjxz(self, url):
        self.driver.get(url)
        current_url = self.driver.current_url.split('/')[2]
        if (current_url == 'oauth.taobao.com'):
            sleep(5)
            print("登陆中......")
            self.driver.switch_to_frame('J_loginIframe')
            inputs = self.driver.find_elements_by_class_name("fm-text")
            useradmin = "13592614512"
            pwd = "Mn261681"
            for i in useradmin:
                inputs[0].send_keys(i)
                sleep(0.1)
            for i in pwd:
                inputs[1].send_keys(i)
            self.driver.find_element_by_css_selector("#login-form > div.fm-btn > button").click()
            sleep(2)
            self.driver.switch_to_frame('baxia-dialog-content')
            # 点击并按住滑块
            btn = self.driver.find_element_by_xpath('//*[@id="nc_1_n1z"]')
            btn.click()
            print(btn)  # left: 0px;
            action = ActionChains(self.driver)
            sleep(2)
            print("hold")
            action.click_and_hold(btn).perform()
            print("holded")
            # 移动滑块
            sleep(2)
            print("move")
            action.move_by_offset(xoffset=300, yoffset=0).perform()
        self.driver.find_element_by_css_selector("#login-form > div.fm-btn > button").click()
        print("登陆成功！")
        sleep(3)
        self.driver.switch_to(-1)
        self.driver.find_element_by_xpath('//*[@id="goodsTitle"]/div[2]/div/input').send_keys('女装')

    def test1(self):
        url = "https://so.571xz.com/hzgoods.htm?webSite=hz&pid=16&sort=comp"
        self.driver.get(url=url)
        list_items = self.driver.find_elements_by_xpath("/html/body/div[4]/div[1]/div[5]/div")
        for i, item in enumerate(list_items):
            sleep(1)
            href = item.find_element_by_css_selector(
                "div.p3.haveSysText > div > div > ul > li:nth-child(1) > a").get_property('href')
            sleep(2)
            self.sjxz(href)
            sleep(50)


if __name__ == '__main__':
    tb = Tb()
    tb.test1()
